home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Examples / Sound / Sources / SndPart.cpp next >
Encoding:
Text File  |  1994-04-21  |  11.8 KB  |  391 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                SndPart.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Author:                Lonnie Millett
  7. //    Creation Date:        3/28/94
  8. //
  9. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //========================================================================================
  12.  
  13. #ifndef SNDPART_H
  14. #include "SndPart.h"
  15. #endif
  16.  
  17. // ----- Framework Includes -----
  18.  
  19. #ifndef FWMEMMGR_H
  20. #include <FWMemMgr.h>
  21. #endif
  22.  
  23. #ifndef FWUTIL_H
  24. #include "FWUtil.h"
  25. #endif
  26.  
  27. #ifndef FWRECT_H
  28. #include "FWRect.h"
  29. #endif
  30.  
  31. // ----- OpenDoc Includes -----
  32.  
  33. #ifndef _SHAPE_
  34. #include <Shape.h>
  35. #endif
  36.  
  37. #ifndef _STDPROPS_
  38. #include <StdProps.h>
  39. #endif
  40.  
  41. #ifndef _STORAGEU_
  42. #include <StorageU.h>
  43. #endif
  44.  
  45. #ifndef _XMPSESSN_
  46. #include <XMPSessM.h>
  47. #endif
  48.  
  49. #ifndef _WINSTAT_
  50. #include <WinStat.h>
  51. #endif
  52.  
  53. // ----- Macintosh Includes -----
  54.  
  55. #if defined(FW_BUILD_MAC) && !defined(__QUICKDRAW__)
  56. #include <Quickdraw.h>
  57. #endif
  58.  
  59. #if defined(FW_BUILD_MAC) && !defined(__SOUND__)
  60. #include <Sound.h>
  61. #endif
  62.  
  63. #if defined(FW_BUILD_MAC) && !defined(__SOUNDINPUT__)
  64. #include <SoundInput.h>
  65. #endif
  66.  
  67. #if defined(FW_BUILD_MAC) && !defined(mathRoutinesIncludes)
  68. #include <math routines.h>
  69. #endif
  70.  
  71. #if defined(FW_BUILD_MAC) && !defined(__OSUTILS__)
  72. #include <OSUtils.h>
  73. #endif
  74.  
  75. #pragma segment sndpart
  76.  
  77. //==============================================================================
  78. //    •• class CSndPart
  79. //==============================================================================
  80.  
  81. //------------------------------------------------------------------------------
  82. //    • CSndPart::CSndPart
  83. //------------------------------------------------------------------------------
  84.  
  85. CSndPart::CSndPart()
  86. {
  87.     fSoundHandle = NULL;
  88. }
  89.  
  90. //------------------------------------------------------------------------------
  91. //    • CSndPart::~CSndPart
  92. //------------------------------------------------------------------------------
  93.  
  94. CSndPart::~CSndPart()
  95. {
  96.     if (fSoundHandle)
  97.         FW_CMemoryManager::FreeSystemHandle(fSoundHandle);
  98. }
  99.  
  100. //------------------------------------------------------------------------------
  101. //    • CSndPart::SetSoundHandle
  102. //------------------------------------------------------------------------------
  103.  
  104. void CSndPart::SetSoundHandle(FW_PlatformHandle soundHandle)
  105. {
  106.     if (fSoundHandle)
  107.         FW_CMemoryManager::FreeSystemHandle(fSoundHandle);
  108.     fSoundHandle = soundHandle;
  109. }
  110.  
  111. //------------------------------------------------------------------------------
  112. //    • CSndPart::CreateControlHandle
  113. //------------------------------------------------------------------------------
  114.  
  115. ControlHandle CSndPart::CreateControlHandle(XMPPlatformWindow window, const FW_CRect& controlRect)
  116. {
  117.     FW_SPlatformRect rect;
  118.     controlRect.AsPlatformRect(rect);
  119.     return ::NewControl(window, &rect, "\pPlay Sound", TRUE, 0, 0, 1, pushButProc, (long)this);
  120. }
  121.  
  122. //----------------------------------------------------------------------------------------
  123. //    • CSndPart::GetContentPropertyValueType
  124. //----------------------------------------------------------------------------------------
  125.  
  126. XMPValueType CSndPart::GetContentPropertyValueType() const
  127. {
  128.     return kSampleSndKind;
  129. }
  130.  
  131. //------------------------------------------------------------------------------
  132. //    • CSndPart::NewFrame
  133. //------------------------------------------------------------------------------
  134.  
  135. FW_CFrame* CSndPart::NewFrame(XMPFrame* xmpFrame, XMPTypeToken presentation)
  136. {
  137. FW_UNUSED(presentation);
  138.     
  139.     CSndFrame *frame = new CSndFrame;
  140.     frame->InitSndFrame(xmpFrame, this);
  141.     return frame;
  142. }
  143.  
  144. //----------------------------------------------------------------------------------------
  145. // CSndPart::NewSelection
  146. //----------------------------------------------------------------------------------------
  147. FW_CSelection* CSndPart::NewSelection()
  148. {
  149.     CSndSelection* sndSelection = new CSndSelection;
  150.     sndSelection->InitSndSelection(this);
  151.     return sndSelection;
  152. }
  153.  
  154. //------------------------------------------------------------------------------
  155. // CSndPart::InternalizeContent
  156. //------------------------------------------------------------------------------
  157. void CSndPart::InternalizeContent(XMPStorageUnit* storageUnit)
  158. {
  159.     storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, this->GetContentPropertyValueType(), 0, kXMPPosUndefined);
  160.     
  161.     unsigned long sndSize = storageUnit->GetSize();
  162.     if (sndSize != 0)
  163.     {
  164.         FW_PlatformHandle soundHandle = FW_CMemoryManager::AllocateSystemHandle(sndSize);
  165.     
  166.         FW_CMemoryManager::LockSystemHandle(soundHandle);
  167.         storageUnit->GetValue(sndSize, (XMPValue)*soundHandle);
  168.         FW_CMemoryManager::UnlockSystemHandle(soundHandle);
  169.         this->SetSoundHandle(soundHandle);
  170.     }
  171. }
  172.  
  173. //----------------------------------------------------------------------------------------
  174. // CSndPart::ExternalizeContent
  175. //----------------------------------------------------------------------------------------
  176. void CSndPart::ExternalizeContent(XMPStorageUnit* storageUnit)
  177. {
  178.     if (fSoundHandle != NULL)
  179.     {
  180.         FW_CMemoryManager::LockSystemHandle(fSoundHandle);
  181.         storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, this->GetContentPropertyValueType(), 0, kXMPPosUndefined);
  182.         storageUnit->DeleteValue(storageUnit->GetSize());
  183.         storageUnit->SetValue(FW_CMemoryManager::GetSystemHandleSize(fSoundHandle), (XMPValue)*fSoundHandle);
  184.         FW_CMemoryManager::UnlockSystemHandle(fSoundHandle);
  185.     }
  186. }
  187.  
  188. //------------------------------------------------------------------------------
  189. //    CSndPart::DoClick
  190. //------------------------------------------------------------------------------
  191.  
  192. void CSndPart::DoClick(const FW_CPoint& where, XMPEventData event)
  193. {
  194.     FW_UNUSED(where);
  195.     FW_UNUSED(event);
  196.     
  197.     if (TestOptionKey(event))
  198.     {
  199.         FW_PlatformHandle soundHandle = NULL;
  200.         FW_SPlatformRect soundBox;
  201.         FW_SPlatformPoint corner;
  202.         
  203.         ::SetRect(&soundBox, 0, 0, 304, 98);        // Size of the sound recorder dialog
  204.         ::CenterRectOnScreen(soundBox, TRUE, TRUE, TRUE);
  205.         ::SetPt(&corner, soundBox.left, soundBox.top);
  206.         
  207.         this->GetSession()->GetWindowState()->DeactivateFrontWindows();
  208.         ::SndRecord(NULL, corner, siGoodQuality, &soundHandle);
  209.         this->GetSession()->GetWindowState()->ActivateFrontWindows();
  210.         
  211.         if (soundHandle)
  212.             this->SetSoundHandle(soundHandle);
  213.     }
  214.     else if (fSoundHandle != NULL)
  215.     {
  216.         FW_CMemoryManager::LockSystemHandle(fSoundHandle);
  217.         SndPlay(NULL, fSoundHandle, TRUE);
  218.         FW_CMemoryManager::UnlockSystemHandle(fSoundHandle);
  219.     }
  220.     else
  221.         SysBeep(10);
  222. }
  223.  
  224. //==============================================================================
  225. //    •• class CSndFrame
  226. //==============================================================================
  227.  
  228. //------------------------------------------------------------------------------
  229. //    • CSndFrame::CSndFrame
  230. //------------------------------------------------------------------------------
  231.  
  232. CSndFrame::CSndFrame()
  233. {
  234. }
  235.  
  236. //------------------------------------------------------------------------------
  237. //    • CSndFrame::InitSndFrame
  238. //------------------------------------------------------------------------------
  239.  
  240. void CSndFrame::InitSndFrame(XMPFrame* frame, FW_CPart* part)
  241. {
  242.     InitCtlMgrFrame(frame, part);
  243.     
  244.     this->SetDroppable(TRUE);
  245. }
  246.  
  247. //------------------------------------------------------------------------------
  248. //    • CSndFrame::~CSndFrame
  249. //------------------------------------------------------------------------------
  250.  
  251. CSndFrame::~CSndFrame()
  252. {
  253. }
  254.  
  255. //------------------------------------------------------------------------------
  256. //    • CSndFrame::UpdateUsedShape
  257. //------------------------------------------------------------------------------
  258.  
  259. void CSndFrame::UpdateUsedShape()
  260. {    
  261.     FW_CPoint controlSize;
  262.     ((CSndPart*)GetPart())->GetControlSize(controlSize);
  263.     FW_SPlatformRect controlRect;
  264.     ::SetRect(&controlRect, 0, 0, FixedToInt(controlSize.x), FixedToInt(controlSize.y));
  265.     
  266.     FW_PlatformRegion rgn = ::NewRgn();
  267.     ::InsetRect(&controlRect, -1, -1);
  268.     ::OpenRgn();
  269.     ::FrameRoundRect(&controlRect, 16, 16);
  270.     ::CloseRgn(rgn);
  271.     
  272.     XMPShape* scratch = ::NewXMPShape(rgn);
  273.     scratch->Intersect(GetXMPFrame()->GetFrameShape());
  274.     GetXMPFrame()->ChangeUsedShape(scratch);
  275. }
  276.  
  277. //==============================================================================
  278. //    •• class CSndSelection
  279. //==============================================================================
  280.  
  281. //---------------------------------------------------------------------------------------
  282. //    • CSndSelection::CSndSelection
  283. //---------------------------------------------------------------------------------------
  284.  
  285. CSndSelection::CSndSelection()
  286. {
  287.     fSndPart = NULL;
  288. }
  289.  
  290. //---------------------------------------------------------------------------------------
  291. //    • CSndSelection::InitMovieSelection
  292. //---------------------------------------------------------------------------------------
  293.  
  294. void CSndSelection::InitSndSelection(CSndPart* sndPart)
  295. {
  296.     InitSelection(sndPart, FALSE, FALSE);
  297.     
  298.     fSndPart = sndPart;
  299. }
  300.  
  301. //---------------------------------------------------------------------------------------
  302. //    • CSndSelection::~CSndSelection
  303. //---------------------------------------------------------------------------------------
  304.  
  305. CSndSelection::~CSndSelection()
  306. {    
  307. }
  308.  
  309. //---------------------------------------------------------------------------------------
  310. //    • CSndSelection::CloseSelection
  311. //---------------------------------------------------------------------------------------
  312.  
  313. void CSndSelection::CloseSelection()
  314. {
  315.     // Nothing to do here
  316. }
  317.  
  318. //---------------------------------------------------------------------------------------
  319. //    • CSndSelection::DoClear
  320. //---------------------------------------------------------------------------------------
  321.  
  322. FW_Boolean CSndSelection::DoClear()
  323. {
  324.     // Nothing to do here
  325.     return FALSE;
  326. }
  327.  
  328. //---------------------------------------------------------------------------------------
  329. //    • CSndSelection::SelectAll
  330. //---------------------------------------------------------------------------------------
  331.  
  332. void CSndSelection::SelectAll()
  333. {
  334.     // Nothing to do here
  335. }
  336.  
  337. //---------------------------------------------------------------------------------------
  338. //    • CSndSelection::IsEmpty
  339. //---------------------------------------------------------------------------------------
  340.  
  341. FW_Boolean CSndSelection::IsEmpty() const
  342. {
  343.     return FALSE;    // We always have something to copy
  344. }
  345.  
  346. //---------------------------------------------------------------------------------------
  347. //    • CSndSelection::ExternalizeSelection
  348. //---------------------------------------------------------------------------------------
  349.  
  350. void CSndSelection::ExternalizeSelection(XMPStorageUnit* storageUnit, FW_CFrame* commandFrame, XMPCloneKind cloneKind)
  351. {
  352.     FW_UNUSED(commandFrame);
  353.     FW_UNUSED(cloneKind);
  354.     
  355.     // Our content type is the standard 'snd ' type
  356.     storageUnit->AddProperty(kXMPPropContents)->AddValue(fSndPart->GetContentPropertyValueType());
  357.     FW_PlatformHandle soundHandle = fSndPart->GetSoundHandle();
  358.     FW_CMemoryManager::LockSystemHandle(soundHandle);
  359.     storageUnit->SetValue(FW_CMemoryManager::GetSystemHandleSize(soundHandle), (XMPValue)*soundHandle);
  360.     FW_CMemoryManager::UnlockSystemHandle(soundHandle);
  361. }
  362.  
  363. //---------------------------------------------------------------------------------------
  364. //    • CSndSelection::InternalizeSelection
  365. //---------------------------------------------------------------------------------------
  366.  
  367. FW_Boolean CSndSelection::InternalizeSelection(XMPStorageUnit* storageUnit, XMPCloneKind cloneKind)
  368. {
  369.     FW_UNUSED(cloneKind);
  370.  
  371.     FW_Boolean internalized = FALSE;
  372.     
  373.     if (storageUnit->Exists(kXMPPropContents, fSndPart->GetContentPropertyValueType(), 0))
  374.     {
  375.         storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, fSndPart->GetContentPropertyValueType(), 0, kXMPPosUndefined);
  376.                        
  377.         unsigned long sndSize = storageUnit->GetSize();
  378.         FW_PlatformHandle soundHandle = FW_CMemoryManager::AllocateSystemHandle(sndSize);
  379.         
  380.         FW_CMemoryManager::LockSystemHandle(soundHandle);
  381.         storageUnit->GetValue(sndSize, (XMPValue)*soundHandle);
  382.         FW_CMemoryManager::UnlockSystemHandle(soundHandle);
  383.         fSndPart->SetSoundHandle(soundHandle);
  384.             
  385.         internalized = TRUE;
  386.     }
  387.         
  388.     return internalized;
  389. }
  390.  
  391.